package com.easy.dfc.service;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.dfc.entity.FactoryInfo;
import com.easy.dfc.entity.ProductInfo;
import com.easy.dfc.entity.UserPara;
import com.easy.dfc.mapper.FactorySetMapper;
import com.easy.dfc.mapper.UserFFFMapper;
import net.sf.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by VULCANTSeries on 2017/5/17.
 */
@Service
public class UserFFFService {

    @Resource
    private UserFFFMapper userFFFMapper;

    @Resource
    private FactorySetMapper factorySetMapper;

    public Result FFFInfo(UserPara para) {
        Result rt = new Result();
        Map<String,Object> map = new HashMap<String,Object>();//
        Integer  favoriteNum = userFFFMapper.getFavoriteNum(para.getUserId(),2);//2-商品
        Integer followNum = userFFFMapper.getFollowNum(para.getUserId(),1);//1-厂商
        Integer footStepNum = userFFFMapper.getFootStepNum(para.getUserId(),2);//2-商品
        map.put("favoriteNum",favoriteNum);
        map.put("followNum",followNum);
        map.put("footStepNum",followNum);
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(map);
        return rt;
    }


    public Result FavoriteProduct(UserPara para) {
        Result rt = new Result();
        List<ProductInfo> favoriteList = userFFFMapper.FavoriteProduct(para.getUserId(),para.getPageIndex(),para.getPageSize());
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(favoriteList);
        return rt;
    }

    public Result FollowProduct(UserPara para) {
        Result rt = new Result();
        List<ProductInfo> favoriteList = userFFFMapper.FollowProduct(para.getUserId(),para.getPageIndex(),para.getPageSize());
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(favoriteList);
        return rt;
    }

    public Result FootStepProduct(UserPara para) {
        Result rt = new Result();
        List<ProductInfo> favoriteList = userFFFMapper.FootStepProduct(para.getUserId(),para.getPageIndex(),para.getPageSize());
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(favoriteList);
        return rt;
    }
    public Result FavoriteFactory(UserPara para) {
        Result rt = new Result();
        List<FactoryInfo> factoryList = userFFFMapper.FavoriteFactory(para.getUserId(),para.getPageIndex(),para.getPageSize());
        List list = new ArrayList();
        for(int i =0; i < factoryList.size(); i++){
            FactoryInfo factory = factoryList.get(i);
            List<Map<String,Object>> productList = factorySetMapper.getNiceProductByFactoryId(factory.getFactoryId());
            Map<String,Object> map = new HashMap<String,Object>();

            BeanMap beanMap = BeanMap.create(factory);
            for (Object key : beanMap.keySet()) {
                map.put(key+"", beanMap.get(key));
            }
            map.put("productList",productList);

            list.add(map);
        }
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(list);
        return rt;
    }

    public Result FollowFactory(UserPara para) {
        Result rt = new Result();
        List<FactoryInfo> factoryList = userFFFMapper.FollowFactory(para.getUserId(),para.getPageIndex(),para.getPageSize());
        List list = new ArrayList();
        for(int i =0; i < factoryList.size(); i++){
            FactoryInfo factory = factoryList.get(i);
            List<Map<String,Object>> productList = factorySetMapper.getNiceProductByFactoryId(factory.getFactoryId());
            Map<String,Object> map = new HashMap<String,Object>();

            BeanMap beanMap = BeanMap.create(factory);
            for (Object key : beanMap.keySet()) {
                map.put(key+"", beanMap.get(key));
            }
            map.put("productList",productList);

            list.add(map);
        }
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(list);
        return rt;
    }

    public Result FootStepFactory(UserPara para) {
        Result rt = new Result();
        List<FactoryInfo> factoryList = userFFFMapper.FootStepFactory(para.getUserId(),para.getPageIndex(),para.getPageSize());
        List list = new ArrayList();
        for(int i =0; i < factoryList.size(); i++){
            FactoryInfo factory = factoryList.get(i);
            List<Map<String,Object>> productList = factorySetMapper.getNiceProductByFactoryId(factory.getFactoryId());
            Map<String,Object> map = new HashMap<String,Object>();

            BeanMap beanMap = BeanMap.create(factory);
            for (Object key : beanMap.keySet()) {
                map.put(key+"", beanMap.get(key));
            }
            map.put("productList",productList);

            list.add(map);
        }
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(list);
        return rt;
    }

    public Result AddFavorite(String userId, String objectId, Integer type) {
        Result rt = new Result();
        userFFFMapper.DelFavorite(userId, objectId, type);
        userFFFMapper.AddFavorite(userId, objectId, type);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result AddFollow(String userId, String objectId, Integer type) {
        Result rt = new Result();
        userFFFMapper.DelFollow(userId, objectId, type);
        userFFFMapper.AddFollow(userId, objectId, type);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result AddFootStep(String userId, String objectId, Integer type) {
        Result rt = new Result();
        userFFFMapper.DelFootStep(userId, objectId, type);
        userFFFMapper.AddFootStep(userId, objectId, type);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result DelFavorite(String userId, String objectId, Integer type) {
        Result rt = new Result();
        userFFFMapper.DelFavorite(userId, objectId, type);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result DelFollow(String userId, String objectId, Integer type) {
        Result rt = new Result();
        userFFFMapper.DelFollow(userId, objectId, type);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result DelFootStep(String userId, String objectId, Integer type) {
        Result rt = new Result();
        userFFFMapper.DelFootStep(userId, objectId, type);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result FavoriteProductOld(UserPara para) {
        Result rt = new Result();
        List<ProductInfo> favoriteList = userFFFMapper.FavoriteProductOld(para.getUserId(),para.getPageIndex(),para.getPageSize());
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(favoriteList);
        return rt;
    }

    public Result FollowFactoryOld(UserPara para) {
        Result rt = new Result();
        List<FactoryInfo> factoryList = userFFFMapper.FollowFactoryOld(para.getUserId(),para.getPageIndex(),para.getPageSize());
        List list = new ArrayList();
        for(int i =0; i < factoryList.size(); i++){
            FactoryInfo factory = factoryList.get(i);
            List<Map<String,Object>> productList = factorySetMapper.getNiceProductByFactoryId(factory.getFactoryId());
            Map<String,Object> map = new HashMap<String,Object>();

            BeanMap beanMap = BeanMap.create(factory);
            for (Object key : beanMap.keySet()) {
                map.put(key+"", beanMap.get(key));
            }
            map.put("productList",productList);

            list.add(map);
        }
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(list);
        return rt;
    }

    public Result HaveFavorite(String userId, String objectId, int type) {
        Result rt = new Result();
        if(userFFFMapper.HaveFavorite(userId, objectId, type)>0)rt.setData(true);
        else rt.setData(false);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }

    public Result HaveFollow(String userId, String objectId, Integer type) {
        Result rt = new Result();
        if(userFFFMapper.HaveFollow(userId, objectId, type)>0)rt.setData(true);
        else rt.setData(false);
        rt.setStatus(EnumConfig.S_CODE.OK);
        return rt;
    }
}
